package com.hulk.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.hulk.cache.ehCacheUtil.EhCacheService;
import com.hulk.common.CalloutServices;
import com.hulk.common.StringUtil;
import com.hulk.dao.UserInfoMapper;
import com.hulk.model.UserInfo;
import com.hulk.service.UserService;

/**
 * 创建时间：2015-1-27 下午5:22:59
 * 
 * @author andy
 * @version 2.2
 */
@Service("userService")
public class UserServiceImpl implements UserService {

	@Resource
	private UserInfoMapper userInfoMapper;
	
	@Autowired
	private EhCacheService ehCacheService;

	
	// 将查询到的数据缓存到myCache中,并使用方法名称加上参数中的userNo作为缓存的key
	// 通常更新操作只需刷新缓存中的某个值,所以为了准确的清除特定的缓存,故定义了这个唯一的key,从而不会影响其它缓存值
	@Cacheable(value = "myCache", key = "'getUser'+#id")
	public String getUser(int id) {
        System.out.println("进来了查询方法......");
		return "我是缓存嗷嗷嗷嗷嗷";
	}

	@Cacheable(value = "myCache2", key = "'getUser'+#id")
	public String getUser2(int id) {
        System.out.println("进来了查询方法......");
		return "我是缓存嗷嗷嗷嗷嗷22222222222";
	}
	

	@Cacheable(value="userCache", key="#id")
	public UserInfo getUserById(int id) {
		return userInfoMapper.selectByPrimaryKey(id);
	}

	
	public List<Map<String,Object>> getUsers() {
		return userInfoMapper.selectAll();
	}
	
	
	public List<UserInfo> selectAll2() {
		return userInfoMapper.selectAll2();
	}
	
	
	@Transactional(rollbackFor = Exception.class)
	public int insertUserInfo(UserInfo userInfo) throws Exception {
		
		int result = userInfoMapper.insertUserInfo(userInfo);
		if(true){
//			throw new Exception("主动异常");
		}
		System.out.println(result);
//		异步调用.........
		AsyncPush push = new AsyncPush(userInfo);
		CalloutServices.executor(push);
		return result;
	}
	/**
	 * 异步调用DEMO
	 * @author CUIJIAJUN
	 *
	 */
	class AsyncPush implements Runnable {
		 private UserInfo userInfo ;
		 
		public AsyncPush(UserInfo userInfo){
				this.userInfo = userInfo;
		}
		
		@Override
		public void run() {
			try {
				for(int i=0;i<100;i++){
					System.out.println("这里是异步调用"+i);
				}
			} catch (Exception e) {
//				logger.error(e.getMessage());
				e.printStackTrace();
			}
		}
	}


	@Override
	public JSONObject removeAllEhCache(String cacheNames) {
		if(StringUtil.isNotEmpty(cacheNames)){
			String[] cacheNameStr = cacheNames.split(",");
			for(String cacheName : cacheNameStr){
				ehCacheService.removeObjectByCacheName(cacheName);
			}
		}else{
			ehCacheService.removeAll();	
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("000","删除缓存成功");
		return jsonObject;
	}

	@Override
	public JSONObject getAllEhCache(String cacheNames) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("000","获取缓存成功");
		if(StringUtil.isNotEmpty(cacheNames)){
			String[] cacheNameStr = cacheNames.split(",");
			for(String cacheName : cacheNameStr){
				Map<Object, Object> map = ehCacheService.getObject(cacheName);
				jsonObject.put(cacheName+"("+map.size()+")",map);
			}
		}else{
			List<Map<Object, Object>> list = ehCacheService.getAllObject();
			for(Map<Object, Object> map : list){
				for(Entry<Object, Object> entry:map.entrySet()){
					Map value=(Map) entry.getValue();
					jsonObject.put(entry.getKey().toString()+"("+value.size()+")",entry.getValue());    
				}   
			}
		}
		return jsonObject;
	}
}
